#include <iostream>

using std::cout;
using std::endl;


void test0()
{
    int num = 1;
    int num2 = 10;
    int * p1 = &num;
    *p1 = 2;//
    
    cout << "*p1: " << *p1 << endl;
    cout << "p1: " << p1 << endl;
    cout << "num: " << num << endl;
    cout << "&num: " << &num << endl;
    cout << "&p1: " << &p1 << endl << endl;

    //p2不能修改所指空间的内容
    //中文: 常量指针
    //英文名：pointer to const
    const int * p2 = &num;
    //*p2 = 10;//error
    p2 = &num2;//p2可以改变指向
    cout << "*p2: " << *p2 << endl;
    cout << "p2: " << p2 << endl;
    cout << "&num2: " << &num2 << endl;
}

void test1()
{
    int num = 1;
    int num2 = 2;

    //英文名：pointer to const
    //中文: 常量指针, const在*号左边
    int const * p1 = &num;
    //*p1 = 10;//error 不能修改指针所指内容的值
    p1 = &num2;//ok 可以改变指向
}

void test2()
{
    int num = 1;
    int num2 = 2;

    //英文名: const pointer
    //中文名: 指针常量, const在*号右边
    int * const p1 = &num;
    *p1 = 10;//ok 可以修改指针所指内容的值
    //p1 = &num2;//error 不可以改变指向
    cout << "*p1:" << *p1 << endl;
    cout << "num:" << num << endl;
}

void test3()
{
    int num = 1, num2 = 10;
    //const的双重限定, 两者都不能进行修改
    const int * const p1 = &num;
    //*p1 = 10;//error
    //p1 = &num2;//error
}

int main()
{
    test2();
    return 0;
}

